package com.glory.hikvisionmobile.activity;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.adapter.SearchDieListAdapter;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.bean.search.SearchDieBean;
import com.glory.hikvisionmobile.bean.search.WorkThingBaseBean;
import com.glory.hikvisionmobile.utils.EditTextEnterUtils;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class Lot_SearchDieActivity extends BaseActivity {

    @BindView(R.id.etDie)
    FreeUI_ClearEditText etDie;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvSearch)
    TextView tvSearch;
    @BindView(R.id.rvSearchDieList)
    RecyclerView rvSearchDieList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.nsScroll)
    NestedScrollView nsScroll;
    private int position = 1;

    private List<SearchDieBean> data = new ArrayList<>();
    private SearchDieListAdapter searchDieListAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_search_die;
    }

    @Override
    protected void initView() {
        rvSearchDieList.setLayoutManager(new LinearLayoutManager(Lot_SearchDieActivity.this));
        rvSearchDieList.setItemAnimator(new DefaultItemAnimator());
        rvSearchDieList.addItemDecoration(new DividerItemDecoration(Lot_SearchDieActivity.this, 1));
        searchDieListAdapter = new SearchDieListAdapter(data);
        searchDieListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        searchDieListAdapter.isFirstOnly(true);
        rvSearchDieList.setAdapter(searchDieListAdapter);
        EditTextEnterUtils.initEnterToUpperCase(etLot);
        EditTextEnterUtils.initEnterToUpperCase(etDie);
        rvSearchDieList.setOnScrollChangeListener((View.OnScrollChangeListener) (view, i, i1, i2, i3) -> {
            if (!rvSearchDieList.canScrollVertically(1)) {
                if (!rvSearchDieList.canScrollVertically(-1)){
                    return;
                }

                if (position > 1){
                    search();
                }

            }
        });

    }

    @Override
    protected void initData() {}

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("DIE关系查询");
        freeUI_titleView.setRightListener("隐藏", v -> {
            initPermission("Refresh", this, new ActionControlListener() {
                @Override
                public void onControlChangeListener() {
                    nsScroll.setVisibility(nsScroll.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
                }
            });



        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    @OnClick(R.id.tvSearch)
    public void onViewClicked() {
        initPermission("Search", this, new ActionControlListener() {
            @Override
            public void onControlChangeListener() {
                String die = etDie.getText().toString().trim();
                String lot = etLot.getText().toString().trim();
                if (FreeApi_StringUtils.isEmpty(die) && FreeApi_StringUtils.isEmpty(lot)){
                    showToast("管壳号或waferID/批次号必须填写一项",false);
                    return;
                }

                if (lot.length() > 0 && lot.length() < 7){
                    showToast("waferID/批次号输入错误",false);
                    return;
                }

                data.clear();
                position = 1;
                searchDieListAdapter.notifyDataSetChanged();
                search();
            }
        });
    }


    private void search() {
        String lot = etLot.getText().toString().trim();
        String waferID = "";
        String lotID = "";
        if (!FreeApi_StringUtils.isEmpty(lot)){
            if (lot.contains("_")) {
                waferID = lot;
            }else {
                lotID = lot;
            }
        }
        JsonObject map = new JsonObject();
        map.addProperty("lotId",lotID);
        map.addProperty("tubeId",etDie.getText().toString().trim());
        map.addProperty("waferId",waferID);
        map.addProperty("pageSize",20);
        map.addProperty("pageNo",position);
        NetUtils.requestDara("HIK.PdaDieRelationShip", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<WorkThingBaseBean>>(
                new TypeToken<BaseResponseBean<WorkThingBaseBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<WorkThingBaseBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    WorkThingBaseBean workThingBaseBean = response.body().getBody();
                    if (workThingBaseBean.getxPdaComponents().size() <= 0){
                        showToast("当前没有更多数据",false);
                    }else {
                        position += 1;
                        nsScroll.setVisibility(View.GONE);
                        searchDieListAdapter.addData(response.body().getBody().getxPdaComponents());
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<WorkThingBaseBean>> response) {
                super.onError(response);
                loadError(response.getException(), "HIK.PdaDieRelationShip");
            }
        });


    }

}
